﻿using System;
using System.Net.Http;
using Crow.Library.Foundation.Common.Helpers;
using Crow.Library.Foundation.Hosting;
using Crow.Library.Host.Configuration;
using Crow.Library.Host.Controllers.BusinessController;
using Crow.Library.Host.Conventions;
using Crow.Library.InjectionContainer;

namespace Crow.Library.Host.Controllers
{
    internal class CrowBusinessControllerSelector : IBusinessControllerSelector
    {
        #region IBusinessControllerSelector Members

        public BusinessControllerBase SelectBusinessController(HttpRequestMessage request, ITypeListHost host,
                                                               INamingConvention convention,
                                                               IInjectionContainer container)
        {
            var url = new UrlParser(request.RequestUri.ToString());
            Type controllerType = host.BusinessTypeList[url.BusinessClass];
            object instance = container.Resolve(controllerType);
            return new CrowBusinessController(instance, url, convention);
        }

        #endregion
    }
}